# common functions
import os
import cgi
from django import shortcuts
from django.http import HttpResponseRedirect
from google.appengine.api import users

def redirect(to):
    redirect_class = HttpResponseRedirect
    return redirect_class(to)

def respond(file, params=None):
    if params is None:
        params = {}
    user = users.get_current_user()
    if user:
        params['user'] = user
        params['sign_out'] = users.CreateLogoutURL('/')
        params['is_admin'] = (users.IsCurrentUserAdmin() and
                              'Dev' in os.getenv('SERVER_SOFTWARE'))
    else:
        params['sign_in'] = users.CreateLoginURL('/')

    template = os.path.join(os.path.dirname(__file__), 'templates', file)
    return shortcuts.render_to_response(template, params)

def render(file, params):
    from django.template import loader, Context

    template = loader.get_template( os.path.join(os.path.dirname(__file__), 'templates', file) )
    context = Context(params)
    return template.render(context)
